//
//  BCTools.m
//  BananaChat
//
//  Created by 于洪东 on 2019/11/1.
//  Copyright © 2019 香蕉. All rights reserved.
//

#import "BCTools.h"
#import <AdSupport/AdSupport.h>
#import <sys/utsname.h>


@implementation BCTools

+ (void)removeValueforKey:(NSString *)key {
    if ([BCTools readValueForKey:key]) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
}

+ (void)writeValue:(id)value forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults]  setObject:[NSKeyedArchiver archivedDataWithRootObject:value] forKey:key];
}

+ (id)readValueForKey:(NSString *)key {
    id obj = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (!obj) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithData:obj];
}

+ (BOOL)isPhoneX {
    BOOL iPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneX = YES;
        }
    }
    return iPhoneX;
}

+ (void)calulateImageFileSize:(UIImage *)image {
    NSData *data = UIImageJPEGRepresentation(image, 1);
    [self calulateImageFileSizeWithData:data];
}

+ (void)calulateImageFileSizeWithData:(NSData *)data {
    double dataLength = [data length] * 1.0;
    NSArray *typeArray = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB",@"ZB",@"YB"];
    NSInteger index = 0;
    while (dataLength > 1024) {
        dataLength /= 1024.0;
        index ++;
    }
    NSLog(@"image = %.3f %@",dataLength,typeArray[index]);
}


+ (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+(UIImage *)attachPlaceHolderImage{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150*ScaleX, 200*ScaleX)];
    backView.backgroundColor = NTLightGrayColor;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150*ScaleX, 200*ScaleX)];
    label.text = @"图片加载中...";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16*ScaleX];
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];
    
    return [self makeImageWithView:backView withSize:CGSizeMake(150*ScaleX, 200*ScaleX)];
}

+ (void)xDic:(NSMutableDictionary *)xDic setObj:(id)i forKey:(NSString*)key
{
    if (i)
    {
        xDic[key] = i;
    }
}
+ (void)xArray:(NSMutableArray *)xArray addObject:(id)i
{
    if (i)
    {
        [xArray addObject:i];
    }
}

+ (NSString *)stringForValue:(id)value
{
    if (value)
    {
        NSString *str = [NSString stringWithFormat:@"%@",value];
        if ([str isEqualToString:@"<null>"])
        {
            return @"";
        }
        else
        {
            return str;
        }
    }
    else
    {
        return @"";
    }
}


+(BOOL)isInspectCheck{
    return NO;
}

+(NSString *)getIDFA{
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // IDFA
}


/*获取当前App的版本号信息*/
+ (NSString *)getAppVersion
{
    NSBundle *currentBundle = [NSBundle mainBundle];
    NSDictionary *infoDictionary = [currentBundle infoDictionary];
    return [infoDictionary objectForKey:@"CFBundleShortVersionString"];
}

/*获取当前App的build版本号信息*/
+ (NSString *)getAppBuildVersion
{
    NSBundle *currentBundle = [NSBundle mainBundle];
    NSDictionary *infoDictionary = [currentBundle infoDictionary];
    return [infoDictionary objectForKey:@"CFBundleVersion"];
}

/*获取当前App的包名信息*/
+ (NSString *)getAppBundleId
{
    NSBundle *currentBundle = [NSBundle mainBundle];
    NSDictionary *infoDictionary = [currentBundle infoDictionary];
    return [infoDictionary objectForKey:@"CFBundleIdentifier"];
}

/*获取当前App的名称信息*/
+ (NSString *)getAppDisplayName
{
    NSBundle *currentBundle = [NSBundle mainBundle];
    NSDictionary *infoDictionary = [currentBundle infoDictionary];
    return [infoDictionary objectForKey:@"CFBundleDisplayName"];
}


/*获取当前设备的横向最大值*/
+ (NSNumber *)getDeviceXMaxValue
{
    CGFloat xoffset = CGRectZero.origin.x;
    xoffset = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    return [NSNumber numberWithFloat:xoffset];
}

/*获取当前设备的纵向最大值*/
+ (NSNumber *)getDeviceYMaxValue
{
    CGFloat yoffset = CGRectZero.origin.x;
    yoffset = CGRectGetHeight([[UIScreen mainScreen] bounds]);
    return [NSNumber numberWithFloat:yoffset];
}

/*获取当前设备的横向分辨率值*/
+ (NSNumber *)getDeviceXResolution
{
    CGFloat width = CGRectZero.size.width;
    CGFloat scale = [[UIScreen mainScreen] scale];
    width = [[[self class] getDeviceXMaxValue] floatValue];
    return [NSNumber numberWithFloat:(width * scale)];
}

/*获取当前设备的纵向分辨率值*/
+ (NSNumber *)getDeviceYResolution
{
    CGFloat height = CGRectZero.size.height;
    CGFloat scale = [[UIScreen mainScreen] scale];
    height = [[[self class] getDeviceYMaxValue] floatValue];
    return [NSNumber numberWithFloat:(height * scale)];
}

/*获取当前设备的型号名称*/
+ (NSString *)getDeviceModel
{
    NSString *platform = nil;
    struct utsname systemInfo;
    uname(&systemInfo);
    platform = [NSString stringWithCString:systemInfo.machine
                                  encoding:NSUTF8StringEncoding];
    return [NSString stringWithFormat:@"%@",platform];
}

/*获取当前设备的操作系统名称*/
+ (NSString *)getDeviceOSName
{
    return @"ios";
}

/*获取当前设备的操作系统版本号*/
+ (NSString *)getDeviceOSVersion
{
    return [[UIDevice currentDevice] systemVersion];
}





@end
